package client.comm.baoding.ui

import android.content.Intent
import androidx.databinding.ObservableField
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.api.bean.UserAddress
import client.comm.baoding.api.event.EventTake
import client.comm.baoding.api.event.EventUserInfo
import client.comm.baoding.databinding.ActivityTradeSelectAddressBinding
import client.comm.baoding.ui.vm.TradeDetailModel
import client.comm.commlib.toast.toast
import com.blankj.utilcode.util.LogUtils
import org.greenrobot.eventbus.EventBus

class TradeAddressActivity : BaseMiddleActivity<TradeDetailModel, ActivityTradeSelectAddressBinding>() {

    val num = ObservableField<String>("")

    override fun getLayout(): Int {
        return R.layout.activity_trade_select_address
    }

    override fun initViewModel(): TradeDetailModel = createViewModel()

    var order_id: String? = null
    var goods_id: String? = null
    var type: Int = 0

    override fun initView() {
        order_id = intent.getStringExtra("order_id")
        goods_id = intent.getStringExtra("goods_id")

        binding.event = this
        binding.vm = viewModel

        type = intent.getIntExtra("type", 0)
        var title = when (type) {
            1 -> "购买"
            2 -> "提货"
            3 -> "摘单"
            4 -> getString(R.string.aixin_xs)
            5 -> "退款"
            else -> ""
        }
        binding.title = title

        viewModel.callUser.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    it.data?.address_list?.let {
                        if (it.size > 0) {
                            binding.addr = it[0]
                        }
                    }
                }
            }
        })
        viewModel.userInfo()

        //摘单
        viewModel.tradePickRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast(it.message)
                    EventBus.getDefault().post(EventTake())
                    finish()
                }
            }
        })

        //提货
        viewModel.takeRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast(it.message)
                    EventBus.getDefault().post(EventTake())
                    finish()

//                    viewModel.myTradeList()
                }
            }
        })
    }


    fun selectAddr() {
        startActivityForResult(Intent(this, AddrMangerActivity::class.java).apply {
            putExtra("select", true)
        }, 0x11)
    }

    fun ok() {
        if (binding.addr == null) {
            toast("请填写收货地址")
            return
        }
        var addressId = binding.addr!!.address_id
        LogUtils.i("addressId=" + addressId)

        when (type) {
            3 -> {
                viewModel.holdTradePick(order_id, goods_id!!, addressId, num.get()!!)
            }
            2 -> {
                viewModel.holdTiHuo(goods_id!!, addressId, num.get()!!)
            }
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            0x11 -> {
                if (resultCode == RESULT_OK) {
                    data?.let {
                        val addr = it.getSerializableExtra("address") as UserAddress.Address?
                        addr?.let {
                            binding.addr = it

                        }
                    }
                }
            }
        }
    }

}